<template>
    <!-- pages/join/join.wxml -->
    <view class="container">
        <image src="/static/img/tg_bk.png" mode="widthFix"></image>
        <view class="form">
            <van-cell-group>
                <van-cell title="所属门店" is-link>
                    <view class="right">
                        <view class="tags">
                            <view class="city store">
                                <picker @change="bindStoreChange" range-key="name" :range="stores">
                                    <view class="picker">
                                        <input type="text" disabled placeholder="请选择" placeholder-class="plh" :value="stores[storeIndex].name" />
                                    </view>
                                </picker>
                            </view>
                        </view>
                    </view>
                </van-cell>
            </van-cell-group>
            <!-- <view class="right"> -->
            <van-cell-group class="coupon">
                <van-field label="团购券码" v-model="groupPayNo" placeholder="请输入团购券码" input-align="right" use-button-slot>
                    <van-button slot="button" size="small" type="primary" @tap.native="scanCode">
                        <view class="iconfont icon-saomiao"></view>
                    </van-button>
                </van-field>
            </van-cell-group>
            <!-- </view> -->
        </view>
        <view class="btns">
            <button hover-class="button-click" class="btn submit bg-primary" @tap="submit">提交验券</button>
        </view>
    </view>
</template>

<script>
// pages/join/join.js
var http = require('../../../utils/http');
var util1 = require('../../../utils/util.js');
const app = getApp();
export default {
    data() {
        return {
            storeId: '',
            groupPayNo: '',
            storeIndex: '',
            stores: [],
            success: false,
            payselectindex: 0,
            region: '',
            name: ''
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad(options) {
        this.getXiaLaListAdmin();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {},
    methods: {
        selectCity() {},
        onChange(event) {},

        //扫码
        scanCode: function () {
            var that = this;
            uni.scanCode({
                //扫描API
                success(res) {
                    //扫描成功
                    //console.log(res) //输出回调信息
                    that.setData({
                        groupPayNo: res.result,
                        payselectindex: 0
                    });
                    uni.showToast({
                        title: '扫码成功',
                        icon: 'success',
                        duration: 1000
                    });
                },
                fail: (res) => {
                    //接口调用失败的回调函数
                    uni.showToast({
                        title: '扫码失败',
                        icon: 'success',
                        duration: 1000
                    });
                }
            });
        },

        bindRegionChange: function (e) {
            //console.log('picker发送选择改变，携带值为', e.detail.value)
            this.setData({
                region: e.detail.value
            });
        },

        submit: function () {
            var that = this;
            if (that.groupPayNo == '' || that.storeId == '' || that.groupPayNo.length == 0) {
                uni.showToast({
                    title: '请填写完整'
                });
                return;
            }
            if (app.globalData.isLogin) {
                http.request(
                    '/member/manager/useGroupNo',
                    '1',
                    'post',
                    {
                        storeId: that.storeId,
                        groupPayNo: that.groupPayNo
                    },
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info(info);
                        if (info.code == 0) {
                            uni.showToast({
                                title: '操作成功',
                                icon: 'success'
                            });
                            setTimeout(() => {
                                uni.navigateBack();
                            }, 1000);
                        } else {
                            uni.showToast({
                                title: '验券失败',
                                icon: 'error'
                            });
                        }
                    },
                    function fail(info) {}
                );
            } else {
                //console.log('未登录失败！')
            }
        },

        // 单选选门店
        bindStoreChange: function (e) {
            //console.log('picker发送选择改变，携带值为', e.detail.value)
            this.setData({
                storeId: this.stores[e.detail.value].id,
                storeIndex: e.detail.value
            });
        },

        //管理员获取门店下拉列表数据
        getXiaLaListAdmin: function (e) {
            var that = this;
            //if (app.globalData.isLogin)
            {
                http.request(
                    '/member/store/getStoreList',
                    '1',
                    'get',
                    {},
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info('下拉门店数据===');
                        console.info(info);
                        if (info.code == 0) {
                            let stores = [];
                            info.data.map((it) => {
                                stores.push({
                                    name: it.key,
                                    id: it.value
                                });
                            });
                            that.setData({
                                stores: stores
                            });
                        } else {
                            uni.showModal({
                                content: '请求服务异常，请稍后重试',
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        }
    }
};
</script>
<style>
@import './scanQr.css';
</style>
